//2009-03-12 -JFD

//添加到cookies
function addCartInCookies(productId,productImg,productNum,productName,productColor,currentPrice,morePriceInfo,inputProductCount,batchCount,handCount){
	var products = get_cookie("products");
	var currentProduct = productId+"|"+productImg+"|"+productNum+"|"+productName+"|"+productColor+"|"+currentPrice+"|"+morePriceInfo+"|"+inputProductCount+"|"+batchCount+"|"+handCount;
	
	if(products == null){
		set_cookie("products",currentProduct,1,"/");
	}else{//COOKIES里有产品；
	
		var product = products.split("&");
		for(var i=0;i<product.length;i++){//循环产品
			var cookiesProduct = product[i].split("|");//取得第一个产品
			var cookiesId = cookiesProduct[0];//产品ID
			var cookiesColor =  cookiesProduct[4];//产品的颜色；
			
			var isExist = false;
			if(cookiesId == productId){//如果ID相等
			
				if(productColor==cookiesColor){//颜色相等；
					var resetCookies;
					var resetCookiesProduct= products.split("&");//获取cookies数组值
					for(var j=0; j<resetCookiesProduct.length;j++){
						var pro = resetCookiesProduct[j].split("|");
						if(j==i){
							if(j==0){
								resetCookies = pro[0]+"|"+pro[1]+"|"+pro[2]+"|"+pro[3]+"|"+pro[4]+"|"+currentPrice+"|"+morePriceInfo+"|"+inputProductCount+"|"+pro[8]+"|"+pro[9];
							}else{
								resetCookies = resetCookies+"&"+pro[0]+"|"+pro[1]+"|"+pro[2]+"|"+pro[3]+"|"+pro[4]+"|"+currentPrice+"|"+morePriceInfo+"|"+inputProductCount+"|"+pro[8]+"|"+pro[9];
							}
						}else{
							if(j==0){
								resetCookies = pro[0]+"|"+pro[1]+"|"+pro[2]+"|"+pro[3]+"|"+pro[4]+"|"+currentPrice+"|"+morePriceInfo+"|"+pro[7]+"|"+pro[8]+"|"+pro[9];
							}else{
								resetCookies = resetCookies+"&"+pro[0]+"|"+pro[1]+"|"+pro[2]+"|"+pro[3]+"|"+pro[4]+"|"+currentPrice+"|"+morePriceInfo+"|"+pro[7]+"|"+pro[8]+"|"+pro[9];
							}
						}
					}
					set_cookie("products",resetCookies,1,"/");
					isExist = true;
					break;
				}
			}
		}
		if(!(isExist)){
			set_cookie("products",products+"&"+currentProduct,1,"/");
		}
	}
	Element.show("cart");
	this.changePrice();
}

//计算价格（产品详细页）
function changePrice(){
	var totalPrice=0.0;
	var totalCount = 0;
	var r_products = get_cookie("products");
	if(r_products != null){
		var r_product = r_products.split("&");
		for(var i=0;i<r_product.length;i++){
			var product = r_product[i].split("|");
			totalPrice = totalPrice + (parseFloat(product[5])*parseInt(product[7]));
			totalCount = totalCount+parseInt(product[7]);
		}
	}
	
	$('totalCount').innerHTML = totalCount;
	$('totalPrice').innerHTML=(parseFloat(totalPrice)).toFixed(1);
	
}


//修改数量
function changeCountInCookies(count,productSelectColor,price,productId){
	this.changeCookies(productSelectColor,count+","+price,productId);
	this.changePrice();
}

//修改COOKIES; 
function changeCookies(productSelectColor,modifyContent,productId){
	var resetCookies="";
	var r_products = get_cookie("products");
	if(r_products != null){
		var cookiesProduct= r_products.split("&");
		for(var i=0; i < cookiesProduct.length;i++){
			var pro = cookiesProduct[i].split("|");
			if(pro[4] == productSelectColor && pro[0] == productId){
				var priceAndMorInfo = modifyContent.split(",");
				if(resetCookies == ""){
					resetCookies = pro[0]+"|"+pro[1]+"|"+pro[2]+"|"+pro[3]+"|"+pro[4]+"|"+priceAndMorInfo[1]+"|"+pro[6]+"|"+priceAndMorInfo[0]+"|"+pro[8]+"|"+pro[9];
				}else{
					resetCookies =resetCookies+"&"+ pro[0]+"|"+pro[1]+"|"+pro[2]+"|"+pro[3]+"|"+pro[4]+"|"+priceAndMorInfo[1]+"|"+pro[6]+"|"+priceAndMorInfo[0]+"|"+pro[8]+"|"+pro[9];	
				}
			}else{
				if(resetCookies == ""){
					resetCookies = cookiesProduct[i]	
				}else{
					resetCookies =resetCookies+"&"+cookiesProduct[i];
				}
			}
		}
	}
	set_cookie("products",resetCookies,1,"/");
}

//删除COOKIES; 
function delProductInCookies(productSelectColor,productId){
	var resetCookies="";
	var r_products = get_cookie("products");
	
	if(r_products != null){
		var cookiesProduct= r_products.split("&");
		for(var j=0;j<cookiesProduct.length;j++){
			var product = cookiesProduct[j].split("|");
			if(!(product[4] == productSelectColor && product[0] == productId)){
				if(resetCookies == ""){
					resetCookies = cookiesProduct[j];
				}else{
					resetCookies = resetCookies+"&"+cookiesProduct[j];
				}
			}
		}
	}
	set_cookie("products",resetCookies,1,"/");
	this.changePrice();
}

//检测是否支持COOKIES; 
function cookieEnable(){   
	var result=false;
	document.cookie = "datianCookies=yes;";
	var test = document.cookie;
	if( test.indexOf("datianCookies=yes;") > -1){
		var date = new Date();    
  		date.setTime (date.getTime()-1);
		document.cookie = "datianCookies=yes;expires="+date.toGMTString();
		return true;
	}
	return result;   
}
//关闭购物车
function closeCart(){
	Element.hide("cart");
}